package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseRole;
import com.tebiecloud.base.server.service.BaseRoleService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统角色-基础信息 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("baserole")
public class BaseRoleController {

    @Autowired
    private BaseRoleService baseRoleService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseRole>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseRole query = pageParams.mapToObject(BaseRole.class);
        QueryWrapper<BaseRole> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseRoleService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseRole> get(@RequestParam("id") Long id) {
        BaseRole entity = baseRoleService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param roleCode 角色编码,
     * @param roleName 角色名称,
     * @param status 状态:0-无效 1-有效,
     * @param roleDesc 角色描述,
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "roleCode", required = false) String roleCode,
        @RequestParam(value = "roleName", required = false) String roleName,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "roleDesc", required = false) String roleDesc,
        @RequestParam(value = "isPersist", required = false) Integer isPersist
    ) {
        BaseRole entity = new BaseRole();
        entity.setRoleCode(roleCode);
        entity.setRoleName(roleName);
        entity.setStatus(status);
        entity.setRoleDesc(roleDesc);
        entity.setIsPersist(isPersist);
        baseRoleService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param roleCode 角色编码
     * @param roleName 角色名称
     * @param status 状态:0-无效 1-有效
     * @param roleDesc 角色描述
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "roleId", required = false) Long roleId,
            @RequestParam(value = "roleCode", required = false) String roleCode,
            @RequestParam(value = "roleName", required = false) String roleName,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "roleDesc", required = false) String roleDesc,
            @RequestParam(value = "isPersist", required = false) Integer isPersist
    ) {
        BaseRole entity = new BaseRole();
        entity.setRoleId(roleId);
        entity.setRoleCode(roleCode);
        entity.setRoleName(roleName);
        entity.setStatus(status);
        entity.setRoleDesc(roleDesc);
        entity.setIsPersist(isPersist);
        baseRoleService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseRoleService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseRoleService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
